package com.interview.javabasic.DataStructrue;/*
@李子宁
Happy,happy everyday!
冲鸭！
*/

public class ArrayBinaryTreeDemo {
    public static void main(String[] args) {
        int arr[] = {1,2,3,4,5,6,7};
        ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
        arrBinaryTree.preOrder(0);
    }
}

//编写一个ArrayBinartTree实现顺序存储
class ArrBinaryTree{
    private int[] arr;//存储数据的数组
    public ArrBinaryTree(int arr[]){
        this.arr = arr;
    }

    //重载方法
    public void preOrder(){
        this.preOrder(0);
    }

    //编写一个方法，完成顺序存储二叉树的前序遍历
   //index数组的下标
    public void preOrder(int index){
        //如果数组为空或者len为0
        if (arr ==null || arr.length==0){
            System.out.println("数组为空~~");
            return;
        }
        System.out.println(arr[index]);
        //向左递归
        if ((index*2+1)<arr.length){
            preOrder(2*index+1);
        }
        //向右递归
        if ((index*2+2)<arr.length){
            preOrder(2*index+2);
        }
    }

    //中序
    public void infixOrder(int index){
        if (arr == null || arr.length == 0){
            System.out.println("数组为空~~");
            return;
        }
        if ((index * 2 + 1)<arr.length){
            infixOrder(index * 2 +1);
        }
        System.out.println(arr[index]);
        if ((index *2 + 1)<arr.length){
            infixOrder(index * 2 + 2);
        }
    }

    //后续
    public void postOrder(int index){
        if (arr == null || arr.length == 0){
            System.out.println("数组为空~~");
            return;
        }
        if ((index * 2 + 1)<arr.length){
            postOrder(index * 2 + 1);
        }
        if ((index * 2 + 2)<arr.length){
            postOrder(index * 2 + 2);
        }
        System.out.println(arr[index]);
    }
}
